/**
 * $Revision: 1.0 $
 * $Author: Geln Yang $
 * $Date: Jun 7, 2010 10:04:05 PM $
 *
 * Author: Geln Yang
 * Date  : Jun 7, 2010 10:04:05 PM
 *
 */
package com.gooex.service.base.impl;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.c4j.freemarker.FreemarkerTools;
import org.c4j.mail.SpringMailSender;
import org.c4j.string.StringTools;
import org.c4j.system.web.hibernate.BaseHibernateService;
import org.c4j.system.web.localize.Localizer;

import com.gooex.domain.base.Order;
import com.gooex.domain.base.OrderCustomer;
import com.gooex.domain.base.OrderVendor;
import com.gooex.domain.common.AppMessage;
import com.gooex.service.base.IOrderNotifier;
import com.gooex.service.common.IAppMessageManager;

import freemarker.template.TemplateException;

/**
 * @author Geln Yang
 * @version 1.0
 */
public class OrderNotifierImpl extends BaseHibernateService implements IOrderNotifier {

	private static final Log logger = LogFactory.getLog(OrderNotifierImpl.class);

	private Map<String, String> templates = new HashMap<String, String>();

	private SpringMailSender mailSender;

	private IAppMessageManager appMessageManager;

	public void sendMessageToCustomers(Order order, String cusTitleTemplate, String cusContentTemplate)
			throws IOException, TemplateException {
		String status = Localizer.defaultLocalizer().getText(order.getStatusText());

		Map<String, Object> root = new HashMap<String, Object>();
		root.put("order", order);
		root.put("orderStatus", status);

		List<OrderCustomer> customers = order.getCustomers();
		if (customers.size() <= 0)
			return;
		for (OrderCustomer customer : customers) {
			root.put("customer", customer);
			String subject = FreemarkerTools.process(cusTitleTemplate, root);
			String content = FreemarkerTools.process(cusContentTemplate, root);
			try {
				String email = customer.getAddress().getEmail();
				if (StringTools.isNotBlankOrNull(email)) {
					mailSender.sendMail(new String[] { email }, null, subject, content);
				}

				String umail = customer.getUser().getEmail();
				AppMessage message = new AppMessage();
				message.setContent(content);
				message.setSubject(subject);
				message.setTo(umail);
				message.setDate(new Date());
				appMessageManager.saveMessage(message);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
	}

	public void sendMessageToVendors(Order order, String cusTitleTemplate, String cusContentTemplate)
			throws IOException, TemplateException {
		List<OrderVendor> vendors = order.getVendors();
		if (vendors.size() <= 0)
			return;

		String status = Localizer.defaultLocalizer().getText(order.getStatusText());

		Map<String, Object> root = new HashMap<String, Object>();
		root.put("order", order);
		root.put("orderStatus", status);

		for (OrderVendor vendor : vendors) {
			root.put("vendor", vendor);
			String subject = FreemarkerTools.process(cusTitleTemplate, root);
			String content = FreemarkerTools.process(cusContentTemplate, root);

			try {
				String email = vendor.getCompany().getEmail();
				if (StringTools.isNotBlankOrNull(email)) {
					mailSender.sendMail(new String[] { email }, null, subject, content);
				}

				String umail = vendor.getCompany().getUser().getEmail();
				AppMessage message = new AppMessage();
				message.setContent(content);
				message.setSubject(subject);
				message.setTo(umail);
				message.setDate(new Date());
				appMessageManager.saveMessage(message);

			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
	}

	public void orderStausChangeNotify(Order order) {
		List<OrderCustomer> customers = order.getCustomers();
		if (customers.size() <= 0)
			return;

		String status = Localizer.defaultLocalizer().getText(order.getStatusText());

		Map<String, Object> root = new HashMap<String, Object>();
		root.put("order", order);
		root.put("orderStatus", status);

		String cusTitleTemplate = templates.get(TEMPLATE_ORDER_STATUS_CUSTOMER_SUBJECT);
		String cusContentTemplate = templates.get(TEMPLATE_ORDER_STATUS_CUSTOMER_CONTENT);

		try {
			sendMessageToCustomers(order, cusTitleTemplate, cusContentTemplate);
		} catch (Exception e) {
			logger.error(e);
		}

		String vdrTitleTemplate = templates.get(TEMPLATE_ORDER_STATUS_VENDOR_SUBJECT);
		String vdrContentTemplate = templates.get(TEMPLATE_ORDER_STATUS_VENDOR_CONTENT);

		try {
			sendMessageToVendors(order, vdrTitleTemplate, vdrContentTemplate);
		} catch (Exception e) {
			logger.error(e);
		}
	}

	public void setMailSender(SpringMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setTemplates(Map<String, String> templates) {
		this.templates = templates;
	}

	public void setAppMessageManager(IAppMessageManager appMessageManager) {
		this.appMessageManager = appMessageManager;
	}

}
